package com.lzy.business.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.base.apis.IncrementApi;
import com.lzy.base.dtos.IncrementDto;
import com.lzy.coupon.apis.MotorcycleSpecServiceApi;
import com.lzy.coupon.dtos.MotorcycleDto;
import com.lzy.coupon.dtos.MotorcycleSpecDto;
import com.lzy.coupon.service.MotorcycleService;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.datasource.multiple.DataSourceHolder;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.*;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.ProjectDto;
import com.lzy.product.dtos.agent.*;
import com.lzy.product.entity.AgentProductImageText;
import com.lzy.product.entity.AgentProtocolProduct;
import com.lzy.product.service.AgentProductImageTextService;
import com.lzy.product.service.AgentProductProjectService;
import com.lzy.product.service.AgentProtocolProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WIN7 on 2018/4/13.
 */
@Component
public class AgentProductFacade {

    @Autowired
    private AgentProductServiceApi agentProductServiceApi;

    @Autowired
    private AgentProductImageTextServiceApi agentProductImageTextService;

    @Autowired
    private AgentProductProjectServiceApi agentProductProjectService;

    @Autowired
    private AgentProtocolProductServiceApi agentProtocolProductServiceApi;

    @Autowired
    private ProjectServiceApi projectServiceApi;

    @Autowired
    MotorcycleService motorcycleService;

    @Autowired
    private IncrementApi incrementApi;

    @Autowired
    private MotorcycleSpecServiceApi motorcycleSpecServiceApi;

    public String productView() {
        return "productLisr.htl";
    }

    public PageInfo<AgentProductDto> queryAgentProductList(Pager pager, AgentProductDto paramDto) {
        return agentProductServiceApi.queryAgentProductList(pager, paramDto);
    }


    /**
     * 新增商品
     */
    public Long createAagntProduct(AgentProductDto agentProductDto) {
        Long productId = agentProductServiceApi.createAagntProduct(agentProductDto);
        //新增产品包含项
        if (agentProductDto.getProductProjectDtos() != null && agentProductDto.getProductProjectDtos().size() > 0) {
            for (int i = 0; i < agentProductDto.getProductProjectDtos().size(); i++) {
                AgentProductProjectDto agentProductProjectDto = DozerUtils.map(agentProductDto.getProductProjectDtos().get(i), AgentProductProjectDto.class);
                agentProductProjectDto.setSerialId(productId);
                agentProductProjectDto.setModel(1);
                //查询汽摩券价格
                DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
                if (agentProductProjectDto.getSpecId() != null) {
                    MotorcycleSpecDto motorcycleSpecDto = motorcycleSpecServiceApi.selectBySpecId(agentProductProjectDto.getSpecId());
                    if (motorcycleSpecDto != null) {
                        agentProductProjectDto.setPrice(motorcycleSpecDto.getPrice());
                    }
                }
                DataSourceHolder.clearDataSource();
                agentProductProjectService.insert(DozerUtils.map(agentProductProjectDto, AgentProductProjectDto.class));
            }
        }
        //新增产品图文项
        if (agentProductDto.getProductImageTextDtos() != null && agentProductDto.getProductImageTextDtos().size() > 0) {
            for (int i = 0; i < agentProductDto.getProductImageTextDtos().size(); i++) {
                AgentProductImageText agentProductImageText = DozerUtils.map(agentProductDto.getProductImageTextDtos().get(i), AgentProductImageText.class);
                agentProductImageText.setProductId(productId);
                agentProductImageTextService.insert(DozerUtils.map(agentProductImageText, AgentProductImageTextDto.class));
            }
        }
        return productId;
    }

    /**
     * 编辑商品
     */
    public Boolean updateAagntProduct(AgentProductDto agentProductDto) {
        //先删除该产品所有包含项与图文  前端输入数据一律视为新增数据
        agentProductImageTextService.deleteByProductId(agentProductDto.getId());
        agentProductProjectService.deleteByProtocolId(agentProductDto.getId(), 1);
        // 新增产品包含项
        if (agentProductDto.getProductProjectDtos() != null && agentProductDto.getProductProjectDtos().size() > 0) {
            for (int i = 0; i < agentProductDto.getProductProjectDtos().size(); i++) {
                AgentProductProjectDto agentProductProjectDto = DozerUtils.map(agentProductDto.getProductProjectDtos().get(i), AgentProductProjectDto.class);
                agentProductProjectDto.setSerialId(agentProductDto.getId());
                agentProductProjectDto.setModel(1);
                //查询汽摩券价格
                DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
                if (agentProductProjectDto.getSpecId() != null) {
                    MotorcycleSpecDto motorcycleSpecDto = motorcycleSpecServiceApi.selectBySpecId(agentProductProjectDto.getSpecId());
                    if (motorcycleSpecDto != null) {
                        agentProductProjectDto.setPrice(motorcycleSpecDto.getPrice());
                    }
                }
                DataSourceHolder.clearDataSource();
                agentProductProjectService.insert(DozerUtils.map(agentProductProjectDto, AgentProductProjectDto.class));
            }
        }
        //新增产品图文项
        if (agentProductDto.getProductImageTextDtos() != null && agentProductDto.getProductImageTextDtos().size() > 0) {
            for (int i = 0; i < agentProductDto.getProductImageTextDtos().size(); i++) {
                AgentProductImageText agentProductImageText = DozerUtils.map(agentProductDto.getProductImageTextDtos().get(i), AgentProductImageText.class);
                agentProductImageText.setProductId(agentProductDto.getId());
                agentProductImageTextService.insert(DozerUtils.map(agentProductImageText, AgentProductImageTextDto.class));
            }
        }
        return agentProductServiceApi.update(agentProductDto);
    }

    /**
     * 删除产品
     */
    public Boolean deleteAagntProduct(Long id) {
        List<AgentProtocolProductDto> list = agentProtocolProductServiceApi.selectByProductId(id);
        Ensure.that(list.size() > 0).isFalse("01001624");
        return agentProductServiceApi.remove(id);
    }

    /**
     * 查询代理商产品包含项信息
     */
    public PageInfo queryBaseProductList() {
        //查询产品包含项
        ProjectDto projectDto = new ProjectDto();
        projectDto.setBusiType(BusinessType.OrderProductType.PRODUCT.getCode());
        projectDto.setBusiType(3);
        PageInfo<ProjectDto> pageInfo = projectServiceApi.queryList(null, projectDto);
        return pageInfo;
    }

    /**
     * 查询汽摩券规格信息
     */
    public List<AgentProductMotorcycleDto> queryMotorcycleList() {
        //查询产品包含项
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        MotorcycleDto dto = new MotorcycleDto();
        List<MotorcycleDto> list = motorcycleService.queryList();
        List<AgentProductMotorcycleDto> resultlist = new ArrayList<>();
        for (MotorcycleDto motorcycleDto : list) {
            List<MotorcycleSpecDto> specDtoList = motorcycleSpecServiceApi.selectByMotorcycleId(motorcycleDto.getId());
            for (MotorcycleSpecDto motorcycleSpecDto : specDtoList) {
                AgentProductMotorcycleDto agentProductMotorcycleDto = new AgentProductMotorcycleDto();
                agentProductMotorcycleDto.setMotorcycleId(motorcycleSpecDto.getId());
                agentProductMotorcycleDto.setMotorcycleName(motorcycleSpecDto.getPrice() + "----" + motorcycleDto.getName());
                resultlist.add(agentProductMotorcycleDto);
            }
        }
        DataSourceHolder.clearDataSource();
        return resultlist;
    }


    /**
     * 根据id查询产品
     *
     * @param id
     * @return
     */
    public AgentProductDto queryAgentProductById(Long id) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        return agentProductServiceApi.queryAgentProductById(id);
    }


    /**
     * 批量删除产品
     */
    public Boolean deleteAagntProductList(Long[] idList) {
        String ids = "";
        Integer n = 0;
        for (Long id : idList) {
            List<AgentProtocolProductDto> list = agentProtocolProductServiceApi.selectByProductId(id);
            Ensure.that(list.size() > 0).isFalse("01001624");
            ids = ids + id + ",";
            n++;
        }
        ids = ids.substring(0, ids.length() - 1);
        return agentProductServiceApi.deleteAagntProductList(ids);
    }

    public List<IncrementDto> incrementList() {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        List<IncrementDto> list= incrementApi.queryAll();
        DataSourceHolder.clearDataSource();
        return list;
    }

}
